package com.zking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zking.dto.OrderDto;
import com.zking.entity.Order;
import com.zking.mapper.IOrderMapper;
import com.zking.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service @RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<IOrderMapper, Order> implements IOrderService {
    @Override
    public Page<Order> search(OrderDto orderDto, int pageNum, int pageSize) {
        QueryWrapper<Order> query = new QueryWrapper<>();
        if (orderDto.getOrderId() != null) {
            // 传递了ID，其他完全没必要了
            query.eq("id", orderDto.getOrderId());
        } else {
            if (orderDto.getPrice() != null) {
                query.eq("price", orderDto.getPrice());
            }
            if (orderDto.getMinPrice() != null) {
                query.ge("price", orderDto.getMinPrice());
            }
            if (orderDto.getMaxPrice() != null) {
                query.le("price", orderDto.getMaxPrice());
            }
            if (orderDto.getUsername() != null) {
                // select o.*, u.username as username from o left join u on o.user_id=o.id
                // query.le("user.username", orderDto.getUsername());
                // 需要联表搜索，使用SQL语句
            }
            if (orderDto.getOrderInfo() != null) {
                query.like("info", orderDto.getOrderInfo());
            }
            if (orderDto.getAddress() != null) {
                query.like("address", orderDto.getAddress());
            }
        }
        return getBaseMapper().selectPage(Page.of(pageNum, pageSize), query);
    }
}
